package be.vdab.listeners;

import javax.servlet.ServletContext;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;

/**
 * Application Lifecycle Listener implementation class MandjeListener
 *
 */
@WebListener
public class MandjeListener implements HttpSessionAttributeListener {
	private static final String MANDJE = "mandje";
	private static final String AANTAL_MANDJES = "aantalMandjes";
	
	public void attributeRemoved(HttpSessionBindingEvent event) {
		if (event.getName().equals(MANDJE)) {
		      verlaagMandjeTeller(event.getSession().getServletContext());
		    }
    }

	public void attributeAdded(HttpSessionBindingEvent event) {
        if (event.getName().equalsIgnoreCase(MANDJE)) {
        	verhoogMandjeTeller(event.getSession().getServletContext());
        }
    }

	private synchronized void verhoogMandjeTeller(ServletContext servletContext){
		Integer mandjeTeller = (Integer) servletContext.getAttribute(AANTAL_MANDJES);
		if (mandjeTeller == null) {
			mandjeTeller = 1;
		} else {
			mandjeTeller++;
		}
		servletContext.setAttribute(AANTAL_MANDJES, mandjeTeller);
	}
	
	private synchronized void verlaagMandjeTeller(ServletContext servletContext){
		Integer mandjeTeller = (Integer) servletContext.getAttribute(AANTAL_MANDJES);
		mandjeTeller--;
		servletContext.setAttribute(AANTAL_MANDJES, mandjeTeller);
	}

	@Override
	public void attributeReplaced(HttpSessionBindingEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	
}
